package org.example.d3_collections_test;

import java.util.*;

public class Room {
    //必须有一幅牌
    private List<Card> allCards = new ArrayList<>();
    public Room(){
        //1.做出54张牌，存入到集合allCards
        //a.点数：个数确定了，类型确定
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        //b.花色：个数确定了，类型确定
        String[] colors = {"♠","♥","♦","♣"};
        int size  = 0;
        //遍历点数再遍历花色，组织牌
        for (String number : numbers) {
            //number = 3
            size++;
            for (String color : colors) {
                //得到一张牌
                Card c = new Card(number,color,size);
                allCards.add(c);//存入了牌
            }
        }
        //单独存入小大王的
        Card c1 = new Card("","小王",++size);//加一以后再存进去
        Card c2 = new Card("","大王",++size);
        Collections.addAll(allCards,c1,c2);
        System.out.println("新牌：" + allCards);
    }
    /**
     * 游戏启动
     */
    public void start(){
        //1.洗牌：allCards
        Collections.shuffle(allCards);
        System.out.println("洗牌后：" + allCards);

        //3.发牌到三个玩家，首先肯定要先定义玩家三个。
        //用集合来装玩家的牌 list（Arraylist 支持排序重复索引） set(TreeSet无索引不重复 )
    List<Card> A = new ArrayList<>();
    List<Card> B = new ArrayList<>();
    List<Card> C = new ArrayList<>();
    //正式发牌给三个玩家，依次发出51张牌，剩余三张牌为底牌
     //
        for (int i = 0; i < allCards.size()-3; i++) {
            Card c = allCards.get(i);
            //判断发牌给谁
            if (i % 3 == 0){
                //A接牌
                A.add(c);
            }else if (i % 3 == 1){
                B.add(c);
            } else if (i % 3 == 2) {
                C.add(c);
            }
        }
        //3.对三个玩家的牌进行排序   设计一个方法
        sortCards(A);
        sortCards(B);
        sortCards(C);


        //4.看牌
        System.out.println("A：" + A);
        System.out.println("B：" + B);
        System.out.println("C：" + C);
//      封装到集合里面
        List<Card> lastThreeCard = allCards.subList(allCards.size() - 3,allCards.size()); //51 52 53
                                        //是索引位置                       //这是截断的区间
        System.out.println("底牌" + lastThreeCard);
        A.addAll(lastThreeCard);
        sortCards(A);
        System.out.println("A抢地主后的牌：" + A);

    }

    /**
     * 集中进行排序
     * @param cards
     */
    private void sortCards(List<Card> cards) {
        Collections.sort(cards, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                return o1.getSize() - o2.getSize(); //升序排序
               // return o2.getSize() - o1.getSize(); //降序排序

            }
        });
    }


}
